﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class MyBackBillOp : Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            MyBackBill SM = obj as MyBackBill;
            int ioldId = SM.MBB_Id;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
            
                    sbSql.Append("UPDATE MyBackBill SET ");
                    sbSql.Append("MBB_Code='" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Supp_Id='" + ValueHandler.GetStringValue(SM.MBB_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Date='" + ValueHandler.GetStringValue(SM.MBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Reason='" + ValueHandler.GetStringValue(SM.MBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Money='" + ValueHandler.GetStringValue(SM.MBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_SUser_Id='" + ValueHandler.GetStringValue(SM.MBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Output='" + ValueHandler.GetStringValue(SM.MBB_Output, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_State='" + ValueHandler.GetStringValue(SM.MBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_PayType='" + ValueHandler.GetStringValue(SM.MBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_AList_Id='" + ValueHandler.GetStringValue(SM.MBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_ARecoId='" + ValueHandler.GetStringValue(SM.MBB_ARecoId, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE MBB_Id=" + SM.MBB_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString()); 
 
                blRes = true;
            }
            catch (Exception ex)
            { 
                LinkService.InsertLog(ESysLogType.Error, "新增/修改返货单信息出错！[" + ex.ToString() + "]", EMenuList.返货管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增返货单信息[" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.返货管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改返货单信息[" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.返货管理);
            }
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            MyBackBill SM = obj as MyBackBill;
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM MyBackBill WHERE MBB_Code='" + SM.MBB_Code + "' AND MBB_Id<>" + SM.MBB_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                db.Open();
                MyBackBill SM = obj as MyBackBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE MyBackBill SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE MBB_Id=" + SM.MBB_Id);
                sbSql.Append(" UPDATE MyBackBillDetail SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE MBBDeta_MBB_Id=" + SM.MBB_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除返货单信息[" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.返货管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除返货单信息失败！[" + ex.ToString() + "]", EMenuList.返货管理);
            }
            db.Close();

            return blRes; ;
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT MBB_Id , MBB_Code,MBB_Supp_Id,B.Supp_Name,MBB_Date,MBB_Money,MBB_SUser_Id,C.SUser_Name,MBB_Output,MBB_Reason,A.State,A.CreateTime,A.CreateMan,case MBB_Output when 0 then '未出库' else '已出库' end as BBill_InputName,A.MBB_PayType,A.MBB_AList_Id,A.MBB_State,A.MBB_ARecoId FROM MyBackBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.MBB_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.MBB_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货列表加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public DataTable GetDetail(object obj)
        {
            try
            {
                db = new DataBase();
                MyBackBill SM = obj as MyBackBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT MBB_Id , MBB_Code,MBB_Supp_Id,B.Supp_Name,MBB_Date,MBB_Money,MBB_SUser_Id,C.SUser_Name,MBB_Output,MBB_Reason,A.State,A.CreateTime,A.CreateMan,case MBB_Output when 0 then '未出库' else '已出库' end as BBill_InputName,A.MBB_PayType,A.MBB_AList_Id,A.MBB_State,A.MBB_ARecoId FROM MyBackBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.MBB_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.MBB_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                sbSql.Append(" AND MBB_Id="+SM.MBB_Id);

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.MBB_AList_Id = ValueHandler.GetIntNumberValue(dr["MBB_AList_Id"]);
                    SM.MBB_ARecoId = ValueHandler.GetIntNumberValue(dr["MBB_ARecoId"]);
                    SM.MBB_Code = ValueHandler.GetStringValue(dr["MBB_Code"]); 
                    SM.MBB_Date = DateTime.Parse(dr["MBB_Date"].ToString());
                    SM.MBB_Id = ValueHandler.GetIntNumberValue(dr["MBB_Id"]);
                    SM.MBB_Money = ValueHandler.GetDecNumberValue(dr["MBB_Money"]);
                    SM.MBB_Output = ValueHandler.GetIntNumberValue(dr["MBB_Output"]);
                    SM.MBB_PayType = ValueHandler.GetIntNumberValue(dr["MBB_PayType"]);
                    SM.MBB_Reason = ValueHandler.GetStringValue(dr["MBB_Reason"]);
                    SM.MBB_State = ValueHandler.GetIntNumberValue(dr["MBB_State"]);
                    SM.MBB_Supp_Id = ValueHandler.GetIntNumberValue(dr["MBB_Supp_Id"]);
                    SM.MBB_SUser_Id = ValueHandler.GetIntNumberValue(dr["MBB_SUser_Id"]);             
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
                db.Close();
                db = new DataBase();
                sbSql = new StringBuilder();
                sbSql.Append(" SELECT MBBDeta_Id  ,MBBDeta_MBB_Id,MBBDeta_PInfo_Id,MBBDeta_Count,MBBDeta_Price,MBBDeta_Money,MBBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM MyBackBillDetail A ");
                sbSql.Append(" LEFT JOIN ProductInfo B ON (A.MBBDeta_PInfo_Id=B.PInfo_Id) ");
                sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                sbSql.Append(" WHERE A.MBBDeta_MBB_Id=" + SM.MBB_Id + " AND A.State=1");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                db.Data.Tables[0].Columns["MBBDeta_Id"].AutoIncrement = true;
                db.Data.Tables[0].Columns["MBBDeta_Id"].AutoIncrementStep = 1;
                if (db.Data.Tables[0].Rows.Count <= 0)
                    db.Data.Tables[0].Columns["MBBDeta_Id"].AutoIncrementSeed = 1;
                else
                    db.Data.Tables[0].Columns["MBBDeta_Id"].AutoIncrementSeed = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Compute("max(MBBDeta_Id)+1", ""));

            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "返货列表加载失败！[" + ex.ToString() + "]", EMenuList.返货管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override object GetModel(object obj)
        {
            MyBackBill SM = obj as MyBackBill;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT MBB_Id , MBB_Code,MBB_Supp_Id,B.Supp_Name,MBB_Date,MBB_Money,MBB_SUser_Id,C.SUser_Name,MBB_Output,MBB_Reason,A.State,A.CreateTime,A.CreateMan,case MBB_Output when 0 then '未出库' else '已出库' end as BBill_InputName,A.MBB_PayType,A.MBB_AList_Id,A.MBB_State,A.MBB_ARecoId FROM MyBackBill A ");
                sbSql.Append(" LEFT JOIN Supplier B ON (A.MBB_Supp_Id=B.Supp_Id)");
                sbSql.Append(" LEFT JOIN SysUser C ON (A.MBB_SUser_Id=C.SUser_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                sbSql.Append(" AND MBB_Id=" + SM.MBB_Id);

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.MBB_AList_Id = ValueHandler.GetIntNumberValue(dr["MBB_AList_Id"]);
                    SM.MBB_ARecoId = ValueHandler.GetIntNumberValue(dr["MBB_ARecoId"]);
                    SM.MBB_Code = ValueHandler.GetStringValue(dr["MBB_Code"]);
                    SM.MBB_Date = DateTime.Parse(dr["MBB_Date"].ToString());
                    SM.MBB_Id = ValueHandler.GetIntNumberValue(dr["MBB_Id"]);
                    SM.MBB_Money = ValueHandler.GetDecNumberValue(dr["MBB_Money"]);
                    SM.MBB_Output = ValueHandler.GetIntNumberValue(dr["MBB_Output"]);
                    SM.MBB_PayType = ValueHandler.GetIntNumberValue(dr["MBB_PayType"]);
                    SM.MBB_Reason = ValueHandler.GetStringValue(dr["MBB_Reason"]);
                    SM.MBB_State = ValueHandler.GetIntNumberValue(dr["MBB_State"]);
                    SM.MBB_Supp_Id = ValueHandler.GetIntNumberValue(dr["MBB_Supp_Id"]);
                    SM.MBB_SUser_Id = ValueHandler.GetIntNumberValue(dr["MBB_SUser_Id"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
            }
            catch (Exception ex)
            { }
            db.Close();
            return SM;
        }
        public List<string> CheckStore(object obj, DataTable dtDetail)
        {
            MyBackBill SM = obj as MyBackBill;
            int ioldId = SM.MBB_Id;
            List<string> LSProduct = new List<string>();
            try
            {
  
                    for (int i = 0; i < dtDetail.Rows.Count; i++)
                    {
                        DataBase _db = new DataBase();
                        StringBuilder _sbSql = new StringBuilder();  
                        _sbSql.Append(" SELECT * FROM StockDetail WHERE SDeta_Stock_Id=1 AND SDeta_RealNumber>=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["MBBDeta_Count"]) + " AND SDeta_PInfo_Id=" + ValueHandler.GetIntNumberValue(dtDetail.Rows[i]["MBBDeta_PInfo_Id"]));
                        SqlHelper.FillDataset(_db.Connection, CommandType.Text, _sbSql.ToString(), _db.Data, new string[] { "dtList" });
                        if (_db.Data.Tables["dtList"].Rows.Count <= 0)
                        {
                            LSProduct.Add(ValueHandler.GetStringValue(dtDetail.Rows[i]["PInfo_Name"]));//库存不足的产品名称
                        } 
                }


            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "校验库存信息出错！[" + ex.ToString() + "]", EMenuList.销售管理);
            }
            db.Close();
            return LSProduct;
        }
        public bool PutOut(object obj, DataTable dtDetail)
        {
            bool blRes = false;
            db = new DataBase();
            try
            {
                MyBackBill SM = obj as MyBackBill;

                StringBuilder sbSql = new StringBuilder();
                db.Open();
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber-" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_Count"]) + ", SDeta_OutputNumber=SDeta_OutputNumber+" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_Count"]) + " WHERE SDeta_Stock_Id=1 AND SDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_PInfo_Id"]));
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                   
                }
                sbSql = new StringBuilder();
                sbSql.Append(" UPDATE MyBackBill SET MBB_Output=1 WHERE MBB_Id="+SM.MBB_Id);
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                db.Transaction.Commit();
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "返货单["+SM.MBB_Code+"]出库完成！", EMenuList.返货管理);
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
            }

            db.Close();
            return blRes;
        }
        public bool Save(object obj, DataTable dtDetail)
        {
            bool blRes = false;
            MyBackBill SM = obj as MyBackBill;
            int ioldId = SM.MBB_Id;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                if (SM.MBB_Id == 0)
                {
                    sbSql.Append("INSERT INTO MyBackBill (MBB_Code,MBB_Supp_Id,MBB_Date,MBB_Reason,MBB_Money,MBB_SUser_Id,MBB_Output,MBB_State,MBB_PayType,MBB_AList_Id,MBB_ARecoId,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("MBB_Code='" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Supp_Id='" + ValueHandler.GetStringValue(SM.MBB_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Date='" + ValueHandler.GetStringValue(SM.MBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Reason='" + ValueHandler.GetStringValue(SM.MBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Money='" + ValueHandler.GetStringValue(SM.MBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_SUser_Id='" + ValueHandler.GetStringValue(SM.MBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Output='" + ValueHandler.GetStringValue(SM.MBB_Output, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_State='" + ValueHandler.GetStringValue(SM.MBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_PayType='" + ValueHandler.GetStringValue(SM.MBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_AList_Id='" + ValueHandler.GetStringValue(SM.MBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_ARecoId='" + ValueHandler.GetStringValue(SM.MBB_ARecoId, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.MBB_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                }
                else
                {
                    sbSql.Append("UPDATE MyBackBill SET ");
                    sbSql.Append("MBB_Code='" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Supp_Id='" + ValueHandler.GetStringValue(SM.MBB_Supp_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Date='" + ValueHandler.GetStringValue(SM.MBB_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Reason='" + ValueHandler.GetStringValue(SM.MBB_Reason, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Money='" + ValueHandler.GetStringValue(SM.MBB_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_SUser_Id='" + ValueHandler.GetStringValue(SM.MBB_SUser_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_Output='" + ValueHandler.GetStringValue(SM.MBB_Output, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_State='" + ValueHandler.GetStringValue(SM.MBB_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_PayType='" + ValueHandler.GetStringValue(SM.MBB_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_AList_Id='" + ValueHandler.GetStringValue(SM.MBB_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("MBB_ARecoId='" + ValueHandler.GetStringValue(SM.MBB_ARecoId, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE MBB_Id=" + SM.MBB_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    ////还原明细的库存
                    //sbSql = new StringBuilder();
                    //sbSql.Append(" SELECT SBDeta_Id  ,SBDeta_SBill_Id,SBDeta_PInfo_Id,SBDeta_Count,SBDeta_Price,SBDeta_Money,SBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM SaleBillDetail A ");
                    //sbSql.Append(" LEFT JOIN ProductInfo B ON (A.SBDeta_PInfo_Id=B.PInfo_Id) ");
                    //sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                    //sbSql.Append(" WHERE A.SBDeta_SBill_Id=" + SM.MBB_Id + " AND A.State=1");
                    //SqlHelper.FillDataset(db.Transaction, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                    //for (int i = 0; i < db.Data.Tables[0].Rows.Count; i++)
                    //{
                    //    sbSql = new StringBuilder();
                    //    sbSql.Append(" UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber+" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " , SDeta_OutputNumber=SDeta_OutputNumber-" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " WHERE SDeta_Stock_Id=1 AND SDeta_PInfo_Id=" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_PInfo_Id"]));
                    //    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    //}

                    //删除明细信息
                    sbSql = new StringBuilder();
                    sbSql.Append(" DELETE FROM MyBackBillDetail WHERE MBBDeta_MBB_Id=" + SM.MBB_Id);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());

                }
                //新增明细信息 

                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    if (dtDetail.Rows[i].RowState == DataRowState.Deleted)
                        continue;
                    sbSql = new StringBuilder();
                    sbSql.Append(" INSERT INTO MyBackBillDetail(MBBDeta_MBB_Id,MBBDeta_PInfo_Id,MBBDeta_Count,MBBDeta_Price,MBBDeta_Money,MBBDeta_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append(" MBBDeta_MBB_Id=" + SM.MBB_Id + ",");
                    sbSql.Append(" MBBDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_PInfo_Id"]) + ",");
                    sbSql.Append(" MBBDeta_Count=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_Count"]) + ",");
                    sbSql.Append(" MBBDeta_Price=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["MBBDeta_Price"]).ToString() + ",");
                    sbSql.Append(" MBBDeta_Money=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["MBBDeta_Money"]).ToString() + ",");
                    sbSql.Append(" MBBDeta_Memo='" + ValueHandler.GetStringValue(dtDetail.Rows[i]["MBBDeta_Memo"], ValueHandler.CharHandlerType.IsRepChar).ToString() + "',");
                    sbSql.Append(" State=1,");
                    sbSql.Append(" CreateTime=getdate(),");
                    sbSql.Append(" CreateMan=" + SM.CreateMan);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString()); 
                }
                db.Transaction.Commit();
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改返货单信息出错！[" + ex.ToString() + "]", EMenuList.返货管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增返货单信息[" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.返货管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改返货单信息[" + ValueHandler.GetStringValue(SM.MBB_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.返货管理);
            } 
            return blRes;
        }
        public bool JS(object obj)
        {
            MyBackBill SModel = obj as MyBackBill;
            AccountRecord SM = new AccountRecord();
            SM.AReco_Code = Coder.CreateCoder(EMenuList.转账管理);
            SM.AReco_Datetime = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd"));
            SM.AReco_InNumber = SModel.MBB_AList_Id;
            SM.AReco_Kind = 2;
            SM.AReco_Man = LinkService.GetCurrentUser().SUser_Id;
            SM.AReco_Noney = SModel.MBB_Money;
            SM.AReco_OutNumber = -1;
            SM.AReco_Reason = "返货单[" + SModel.MBB_Code + "]结算，结算金额：" + SModel.MBB_Money;
            SM.AReco_Type = 1;
            SM.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SM.CreateTime = System.DateTime.Now;
            SM.State = 1;
            AccountRecordOp accountRecordManager = new AccountRecordOp();
            bool blRes = accountRecordManager.Save(SM);
            if (blRes)
            {
                try
                {
                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE MyBackBill SET MBB_State = 1 , MBB_ARecoId=" + SM.ARecoId + " WHERE MBB_Id=" + SModel.MBB_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                    blRes = true;
                    LinkService.InsertLog(ESysLogType.Operation, "返货单[" + SModel.MBB_Code + "]结算完成！", EMenuList.返货管理);
                }
                catch (Exception ex)
                {
                    blRes = false;
                }
                db.Close();
            }
            return blRes;
        }
    }
}
